/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#ifndef __MaCI_EmergencyStopMainFrame__
#define __MaCI_EmergencyStopMainFrame__

/**
@file
Subclass of MainFrame, which is generated by wxFormBuilder.
*/

#include "MaCI_EmergencyStop_GUI.h"
#include "MaCI_EmergencyStopDeactivationDialog.h"
#include "MaCI_EmergencyStopAbout_Dialog.h"
#include "MaCI_EmergencyStopInformation_Dialog.h"
#include "MaCI_EmergencyStopHelp_Dialog.h"
#include "owndebug.h"
#include "Component.hpp"
#include "ownutils.h"
#include "EmergencyStopClient.hpp"
#include "sync.hpp"
#include "executor.hpp"
#include "KeyWatcher.hpp"
#include "USBPanicButtonProtocol.hpp"
#include <stdio.h>
#include <stdlib.h>
// Custom events.
BEGIN_DECLARE_EVENT_TYPES()
DECLARE_EVENT_TYPE(MY_EVENT_EMERGENCYSTOPEVENT, -1);
DECLARE_EVENT_TYPE(MY_EVENT_EMERGENCYSTOPDATAEVENT, -1);
DECLARE_EVENT_TYPE(MY_EVENT_CONNECTSERVICES, -1);
DECLARE_EVENT_TYPE(MY_EVENT_ACTIVATE_EMERGENCYSTOP, -1);
DECLARE_EVENT_TYPE(MY_EVENT_UPDATE_INFOTEXT, -1);
END_DECLARE_EVENT_TYPES();
/** Implementing MainFrame */

class MaCI_EmergencyStopMainFrame : private gim::CSync,
                                    private gim::CThread,
                                    public MainFrame,
                                    public Gimbo::CComponent,
                                    public MaCI::EmergencyStop::CEmergencyStopClientCallback
                                   
{
  DECLARE_EVENT_TABLE();
 protected:
  // Handlers for MainFrame events.
  void OnClose( wxCloseEvent& event );
  void OnQuit( wxCommandEvent& event );
  void DoClosing(void);
  void OnAbout( wxCommandEvent& event );
  void OnEmergencyStopClicked( wxCommandEvent& event );
  void OnDeactivate( wxCommandEvent& event );
  void OnKeyBind( wxCommandEvent& event );
  void OnEmergencyStopQuestionResponse(wxCommandEvent& event);
  void OnTestButtonClicked( wxCommandEvent& event );
  void OnServiceAssignment(const Gimbo::TServiceAssignmentList &aServiceList);
  void OnHowTo( wxCommandEvent& event );
  //Derived from emergency stop client callback
  void OnEmergencyStopDataEvent(const MaCI::EmergencyStop::CEmergencyStopData &aData, 
                                const unsigned int aDataSequence,
                                MaCI::EmergencyStop::CEmergencyStopClient &aEmergencyStopClient);

 public:
  /** Constructor */
  MaCI_EmergencyStopMainFrame( wxWindow* parent );

  bool Initialize(gimi::GIMI *aGimiPtr, int aGimboID, int aReportID);
 private: 
  void OnNewEmergencyStopDataEvent(wxCommandEvent &aEvent);
  void OnConnectService(wxCommandEvent &aEvent);
  void OnUpdateInfoText(wxCommandEvent &aEvent);
  bool DisconnectService();
  int ThreadFunction(const int aThreadNumber);
  void CheckForUSBPanicButtonAndStartIt();
  unsigned int iTimeoutTime;
  Gimbo::TServiceAssignmentList iServiceList;
  MaCI::MaCICtrl::CMaCICtrlClient *iMaCICtrlClient;
  std::string iMaCISL;
  bool iConnected;
  std::string iVersion;
  enum EQuestionType{
    KQuestionDeactivateEmergencyStop = 0,
    KQuestionDisconnectFromServer = 1,
  };

  gimi::GIMI *iGIMIPtr;
  MaCI::EmergencyStop::CEmergencyStopClient *iEmergencyStopClient;
  MaCI::EmergencyStop::CEmergencyStopData iEmergencyStopData;
  CExecutor iExecutorKeyboardWatcher;
  CExecutor iExecutorUSBPanicButton;
  bool keyWatcherExecuted;
  bool iUSBPanicButtonExecuted;
  bool iUSBThreadRunning;
  int iReadPipeIntToKeyboard;  
  int iWritePipeIntToKeyboard;
  int iReadPipeIntToUSBPanicButton;  
  int iWritePipeIntToUSBPanicButton;
  bool iKeyBindingInformationSet;
  MaCI_EmergencyStopInformation_Dialog *iBindingInfo;
  MaCI_EmergencyStopInformation_Dialog *iTestBindingInfo;
  bool iThreadRunning;
  bool iCanBindKey;

};

#endif // __MaCI_EmergencyStopMainFrame__
